package objects;

import java.util.ArrayList;
import java.util.Map;
import java.util.TreeMap;

import common.SQLManager;
import common.World;

public class Guild {
	private int _id;
	private String _name = "";
	private String _emblem = "";
	private Map<Integer,GuildMember> _members = new TreeMap<Integer,GuildMember>();
	private int _lvl;
	private long _xp;
	
	public static class GuildMember
	{
		private int _guid;
		private Guild _guild;
		private String _name;
		private int _level;
		private int _gfx;
		private byte _align;
		private int _rank = 0;
		private byte _pXpGive = 0;
		private long _xpGave = 0;
		private int _rights = 0;
		
		public GuildMember(int gu,Guild g,String name,int lvl,int gfx,int r,long x,byte pXp,int ri,byte a)
		{
			_guid = gu;
			_guild = g;
			_name = name;
			_level = lvl;
			_gfx = gfx;
			_rank = r;
			_xpGave = x;
			_pXpGive = pXp;
			_rights = ri;
			_align = a;
		}
		
		public int getAlign()
		{
			return _align;
		}
		
		public int getGfx()
		{
			return _gfx;
		}
		
		public int getLvl()
		{
			return _level;
		}
		
		public String getName()
		{
			return _name;
		}
		
		public int getGuid()
		{
			return _guid;
		}
		public int getRank()
		{
			return _rank;
		}
		
		public Guild getGuild()
		{
			return _guild;
		}

		public String parseRights()
		{
			return Integer.toString(_rights,36);
		}

		public int getRights()
		{
			return _rights;
		}

		public long getXpGave() {
			return _xpGave;
		}

		public int getPXpGive()
		{
			return _pXpGive;
		}

		public Personnage getPerso()
		{
			return World.getPersonnage(_guid);
		}

		public boolean canInvite()
		{
			if(_rank == 1)return true;
			return false;
		}
		public boolean canKick()
		{
			if(_rank == 1)return true;
			return false;
		}

		public void setRank(int i)
		{
			_rank = i;
		}
	}
	
	public Guild(Personnage owner,String name,String emblem)
	{
		_id = World.getNextHighestGuildID();
		_name = name;
		_emblem = emblem;
		_lvl = 1;
		_xp= 0;
	}
	public Guild(int id,String name, String emblem,int lvl,long xp)
	{
		_id = id;
		_name = name;
		_emblem = emblem;
		_xp = xp;
		_lvl = lvl;
	}
	public GuildMember addMember(int guid,String name,int lvl,int gfx,int r,byte pXp,long x,int ri,byte a)
	{
		GuildMember GM = new GuildMember(guid,this,name,lvl,gfx,r,x,pXp,ri,a);
		_members.put(guid,GM);
		return GM;
	}
	public GuildMember addNewMember(Personnage p)
	{
		GuildMember GM = new GuildMember(p.get_GUID(),this,p.get_name(),p.get_lvl(),p.get_gfxID(),0,0,(byte) 0,0,p.get_align());
		_members.put(p.get_GUID(),GM);
		return GM;
	}
	public int get_id()
	{
		return _id;
	}


	public String get_name() {
		return _name;
	}
	
	public String get_emblem()
	{
		return _emblem;
	}
	public long get_xp()
	{
		return _xp;
	}
	public int get_lvl()
	{
		return _lvl;
	}
	public int getSize()
	{
		return _members.size();
	}
	public String parseMembersToGM()
	{
		String str = "";
		for(GuildMember GM : _members.values())
		{
			String online = "0";
			if(GM.getPerso() != null)if(GM.getPerso().isOnline())online = "1";
			if(str.length() != 0)str += "|";
			str += GM.getGuid()+";";
			str += GM.getName()+";";
			str += GM.getLvl()+";";
			str += GM.getGfx()+";";
			str += GM.getRank()+";";
			str += GM.getXpGave()+";";
			str += GM.getPXpGive()+";";
			str += GM.getRights()+";";
			str += online+";";
			str += GM.getAlign()+";";
			str += "0";//FIXME LastConnection
		}
		return str;
	}
	public ArrayList<Personnage> getMembers()
	{
		ArrayList<Personnage> a = new ArrayList<Personnage>();
		for(GuildMember GM : _members.values())a.add(GM.getPerso());
		return a;
	}
	public void removeMember(int guid)
	{
		_members.remove(guid);
		SQLManager.DEL_GUILDMEMBER(guid);
	}
}
